package com.sh.data.engine.util;

import org.apache.commons.lang3.StringUtils;

import java.net.SocketTimeoutException;
import java.sql.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

/* loaded from: hufu-online-task-client.jar:BOOT-INF/classes/com/xuelangyun/util/MysqlUtil.class */
public class MysqlUtil {
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new Error(e);
        }
    }

    public static void testConnection(
        String jdbcUrl, String username, String password, Integer timeout) throws SQLException {
        if (timeout == null) {
            timeout = 10;
        }
        int loginTimeout = DriverManager.getLoginTimeout();
        DriverManager.setLoginTimeout(timeout); // 再设置成默认值，以防对其他地方有影响
        try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
        } catch (SQLException e) {
            throw new SQLException(toReadableMsg(e), e);
        } finally {
            DriverManager.setLoginTimeout(loginTimeout);
        }
    }

    public static Connection createConnection(String jdbcUrl, String username, String password)
        throws SQLException {
        Properties props = new Properties();
        props.put("user", username);
        if (StringUtils.isNotBlank(password)) {
            props.put("password", password);
        }
        props.put("useInformationSchema", "true");
        return DriverManager.getConnection(jdbcUrl, props);
    }

    public static List<String> getTableList(Connection conn) throws SQLException {
        String sql = "SHOW TABLES";
        List<String> list = new LinkedList<>();
        try (PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                list.add(resultSet.getString(1));
            }
        }
        return list;
    }

    public static List<String> getViewList(Connection conn) throws SQLException {
        return Collections.emptyList();
    }

    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }

    private static Throwable getThrowable(Throwable t) {
        while (t.getCause() != null) {
            t = t.getCause();
        }
        return t;
    }

    private static String toReadableMsg(SQLException e) {
        if (getThrowable(e) instanceof SocketTimeoutException) {
            return "连接失败，请检查数据库服务是否可用或数据库链接是否正确";
        }
        return "连接失败，请检查用户名、密码、数据库名等参数是否正确";
    }

    public static PreparedStatement createPreparedStatement(
        Connection conn, String sql, Object... params) throws SQLException {
        PreparedStatement statement = conn.prepareStatement(sql);
        if (params == null || params.length == 0) {
            return statement;
        }
        for (int index = 0; index < params.length; index++) {
            Object param = params[index];
            if (param instanceof String) {
                statement.setString(index + 1, (String) param);
            } else if (param instanceof Long) {
                statement.setLong(index + 1, ((Long) param).longValue());
            } else if (param instanceof Integer) {
                statement.setInt(index + 1, ((Integer) param).intValue());
            } else {
                statement.setString(index + 1, param.toString());
                // throw new SQLException("not support param type " + param.getClass().getSimpleName());
            }
        }
        return statement;
    }

    public static Boolean doInsert(Connection con, String sql) throws SQLException {
        try {
            Boolean valueOf = Boolean.valueOf(createPreparedStatement(con, sql, new Object[0]).execute());
            closeConnection(con);
            return valueOf;
        } catch (Throwable th) {
            closeConnection(con);
            throw th;
        }
    }
}
